package com.pug.controller.hotel;

import com.pug.bo.HotelBo;
import com.pug.bo.HotelTypeBo;
import com.pug.bo.PageHotel;
import com.pug.controller.BaseController;
import com.pug.pojo.Hotel;
import com.pug.service.hotel.HotelServiceImpl;
import com.pug.service.hotel.HotelTypeServiceImpl;
import com.pug.utils.fn.asserts.Vsserts;
import com.pug.vo.HotelVo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: lqm
 * create: 2022/3/22 17:20
 * Lover: lqx
 *
 * @see
 */
@RestController
public class LoadController extends BaseController {
    @Resource
    HotelServiceImpl hotelService;
    @Resource
    HotelTypeServiceImpl hotelTypeService;
    /**
     * 使用自定义分页去加载酒店数据（查询出来的，且进行sql优化）
     * @param hotelVo
     * @return
     */
    @PostMapping("/hotel/load")
    public PageHotel<Hotel> loadMessage(@RequestBody HotelVo hotelVo){
        int total = hotelService.total(hotelVo);
        int pages=0;
        if(total%hotelVo.getPageSize()==0){
            pages=total/hotelVo.getPageSize();
        }
        else pages=total/hotelVo.getPageSize()+1;
        List<Hotel> hotels = hotelService.selectHotelByKeyword(hotelVo);
        PageHotel<Hotel> pageHotel = new PageHotel<>();
        pageHotel.setTotal(total);
        pageHotel.setPages(pages);
        pageHotel.setRecords(hotels);
        return pageHotel;
    }
    //加载酒店详细信息
    @PostMapping("/hotel/detail/{id}")
    public HotelBo loadDetailedMessage(@PathVariable("id") String id){
//        Hotel byId = hotelService.getById(id);
//        return hotelService.getHotels(id);
        Vsserts.isNotEmptyEx(id, "酒店ID不能为空!!");
        HotelBo hotels = hotelService.getHotels(new Long(id));
        if(Vsserts.isNotNull(hotels) && Vsserts.isNotEmpty(hotels.getImglists())){
            List<String> stringList = new ArrayList<>();
            // 把主图放第一张
            stringList.add(hotels.getImg());
            String[] split = hotels.getImglists().split(",");
            for (String s : split) {
                stringList.add(s);
            }
            // 其他的图集往后面放
            hotels.setImagesList(stringList);
            // 把原来的清空
            hotels.setImglists("");
        }
        //处理该酒店的房型
        List<HotelTypeBo> byHotelId = hotelTypeService.findByHotelId(new Long(id));
        hotels.setHotelTypes(byHotelId);
        //处理该酒店的服务项
        return hotels;
    }


}
